package cn.sciento.farm.devicemanager.fjhsxq.upload.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevCollection;
import cn.sciento.farm.devicemanager.dao.entity.DevDevice;
import cn.sciento.farm.devicemanager.dao.entity.FjhsxqUploadLog;
import cn.sciento.farm.devicemanager.dao.mapper.FjhsxqUploadLogMapper;
import cn.sciento.farm.devicemanager.fjhsxq.exception.NullTenantException;
import cn.sciento.farm.devicemanager.fjhsxq.model.common.config.UploadConfig;
import cn.sciento.farm.devicemanager.fjhsxq.model.common.sensor.SensorTypeEnum;
import cn.sciento.farm.devicemanager.fjhsxq.model.common.upload.*;
import cn.sciento.farm.devicemanager.fjhsxq.upload.UploadInfoService;
import cn.sciento.farm.devicemanager.service.CollectionService;
import cn.sciento.farm.devicemanager.service.DeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Walton
 * @since 2020/9/25 14:51
 */
@Service
public class UploadInfoServiceImpl implements UploadInfoService {

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private FjhsxqUploadLogMapper fjhsxqUploadLogMapper;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private UploadConfig uploadConfig;

    @Override
    public UploadSensorTotal uploadSensorData(){
        List<DevCollection> collectionList = collectionService.getByTenantId(uploadConfig.getTenantId());
        if (collectionList.size() < 1 ) {
            throw new NullTenantException("该租客不存在: "+uploadConfig.getTenantId());
        }
        UploadSensorTotal uploadSensorTotal = new UploadSensorTotal();
        List<String> euiList = getMachineCodeList(collectionList);
        List<UploadSensorData> uploadSensorDatas = new ArrayList<>();
        for (String eui : euiList) {
            UploadSensorData uploadSensorData = new UploadSensorData();
            List<DevCollection> collections = collectionService.getByEui(eui);
            List<MachineInfo> machineInfos = getMachineInfos(collections, eui);
            DevDevice devDevice = deviceService.getById(eui);
            uploadSensorData.setLandCode(String.valueOf(devDevice.getLandId()));
            uploadSensorData.setEcCode(uploadConfig.getEcCode());
            uploadSensorData.setLandName("");
            uploadSensorData.setMachineInfoList(machineInfos);
            uploadSensorDatas.add(uploadSensorData);
        }
        uploadSensorTotal.setUploadSensorDatas(uploadSensorDatas);
        return uploadSensorTotal;
    }

    private List<String> getMachineCodeList(List<DevCollection> collectionList){
        List<String> arrList = new ArrayList<>();
        for (DevCollection devCollection : collectionList) {
            String eui = devCollection.getEui();
            if (!arrList.contains(eui)) {
                arrList.add(eui);
            }
        }
        return arrList;
    }

    private List<MachineInfo> getMachineInfos(List<DevCollection> collections, String eui){
        List<SensorData> sensors= new ArrayList<>();
        MachineInfo machineInfo = new MachineInfo();
        List<MachineInfo> machineInfos = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        long nowDate = date.getTime() - 2 * 60 * 60 * 1000;
        String format = sdf.format(date);
        for (int i = 0; i < collections.size(); i++) {
            DevCollection collection = collections.get(i);
            date.setTime(collection.getLastTime());
            String code = SensorTypeEnum.getCode(collection.getType());
            if (StringUtils.isNotEmpty(code) && nowDate < collection.getLastTime()) {
                SensorData sensor = new SensorData();
                sensor.setSensorCode(String.valueOf(collection.getId()));
                sensor.setSensorType(code);
                sensor.setValue(String.valueOf(collection.getLastData()));
                sensor.setCollectTime(sdf.format(date));
                sensor.setUploadTime(format);
                sensors.add(sensor);
            }
        }
        if (sensors.size() < 1){
            return machineInfos;
        }
        machineInfo.setMachineCode(eui);
        machineInfo.setSensorDataList(sensors);
        machineInfos.add(machineInfo);
        return machineInfos;
    }

    @Override
    public int save(FjhsxqUploadLog fjhsxqUploadLog) {
        fjhsxqUploadLog.setTenantId(uploadConfig.getTenantId());
        return fjhsxqUploadLogMapper.insert(fjhsxqUploadLog);
    }

    @Override
    public long batchDeleteLogByCreateTime(long createTime) {
        return fjhsxqUploadLogMapper.batchDeleteLogByCreateTime(createTime);
    }

    // 1.土地信息上传
    @Override
    public UploadLandData uploadLandInfoList(){
        LandInfo landInfo = new LandInfo();
        List<LandInfo> landInfos = new ArrayList<>();
        UploadLandData uploadLandData = new UploadLandData();
        landInfo.setArea("0.52");
        landInfo.setParentLandCode("-1");
        landInfo.setLandCode("5");
        landInfo.setLandName("A1区");
        landInfo.setLandType("4"); // 1-普通农田 2-大棚 3-基础设施 4-养殖舍 5-厂房
        landInfo.setLandPoints("24.714715635349673,117.7525202951847");
        landInfos.add(landInfo);
        uploadLandData.setOprType("1"); // 1-新增 2-修改 3-删除
        uploadLandData.setEcCode(uploadConfig.getEcCode());
        uploadLandData.setLandInfoList(landInfos);
        return uploadLandData;
    }


}

